package top.jiangqiang.qyblog.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import top.jiangqiang.qyblog.common.base.controller.BaseController;
import top.jiangqiang.qyblog.common.base.query.PageResult;
import top.jiangqiang.qyblog.common.base.response.Result;
import top.jiangqiang.qyblog.common.constant.RequestURLConstants;
import top.jiangqiang.qyblog.core.domain.dto.*;
import top.jiangqiang.qyblog.core.domain.vo.AdminUserVo;
import top.jiangqiang.qyblog.core.service.IAdminUserService;

/**
 * @author jiangqiang
 * @date 2022-11-20
 */
@RestController
@RequestMapping(RequestURLConstants.USER_CONTROLLER_PREFIX)
@RequiredArgsConstructor
@Tag(name = "后台用户接口", description = "多次登录不会使之前的token失效，只有修改密码才会使之前的所有token失效")
public class AdminUserController extends BaseController {
    private final IAdminUserService userService;

    @GetMapping
    @Operation(summary = "根据条件查询文章列表", description = "可以实现的业务场景有：</br>" +
            "1.根据文章标题和文章状态进行筛选" +
            "2.查询指定标签下的文章" +
            "3.查询指定分类下的文章")
    public Result<PageResult<AdminUserVo>> list(@ParameterObject AdminUserQueryDto adminUserQueryDto) {
        return userService.findByCondition(adminUserQueryDto);
    }

    @Operation(summary = "注册时校验用户名是否可用", description = "用于注册或修改用户名之前校验用户名是否可用，可用返回true")
    @GetMapping("/validateUsername")
    public Result<Boolean> validateUsername(@Parameter(name = "username", description = "长度在5-32位之间，由字母、数字和下划线组成") @NotBlank(message = "用户名不能为空") String username) {
        return userService.validateUsername(username);
    }

    @Operation(summary = "用户注册", description = "注册成功会自动登录，并返回token")
    @PostMapping(value = "/register", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<String> registerUser(@RequestBody AdminUserRegisterDto adminUserRegisterDto) {
        return userService.registerUser(adminUserRegisterDto);
    }

    @Operation(summary = "用户登录", description = "支持json提交，登录返回token")
    @PostMapping(value = "/login", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<String> loginForJson(@RequestBody AdminUserLoginDto adminUserLoginDto) {
        return userService.login(adminUserLoginDto);
    }

    @Operation(summary = "用户登录", description = "支持表单提交，登录返回token")
    @PostMapping(value = "/login", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result<String> loginForForm(AdminUserLoginDto adminUserLoginDto) {
        return userService.login(adminUserLoginDto);
    }

    @Operation(summary = "退出登录", description = "退出登录")
    @PostMapping(value = "/logout")
    public Result<String> logout() {
        return userService.logout();
    }

    @Operation(summary = "查询当前是否登录", description = "查询登录状态，当前已经登录返回true，否则返回false 此接口暂时没啥用")
    @GetMapping("/isLogin")
    public Result<Boolean> isLogin() {
        return Result.ok(StpUtil.isLogin());
    }

    @Operation(summary = "查询用户信息", description = "获取当前登录用户的信息")
    @GetMapping("/info")
    public Result<AdminUserVo> getUserInfo() {
        return userService.getUserInfo();
    }

    @Operation(summary = "修改用户信息", description = "修改当前用户信息")
    @PutMapping("/info")
    public Result<AdminUserVo> updateUserInfo(@RequestBody AdminUserUpdateInfoDto adminUserUpdateInfoDto) {
        return userService.updateUserInfo(adminUserUpdateInfoDto);
    }

    @Operation(summary = "修改密码", description = "密码修改成功后会返回新的token，原token失效")
    @PutMapping("/password")
    public Result<String> registerUser(@RequestBody AdminUserUpdatePasswordDto adminUserUpdatePasswordDto) {
        return userService.updateUserPassword(adminUserUpdatePasswordDto);
    }

}
